<?php
include 'config.php';

// Dohvati predračune – dokument_type_id = 2
$sql = "SELECT dh.id,
               dh.doc_number,
               dh.doc_date,
               dh.total_gross,
               dh.doc_status,
               k.naziv_klijenta
        FROM document_header dh
        LEFT JOIN klijenti k ON dh.client_id = k.sifra
        WHERE dh.document_type_id = 2
        ORDER BY dh.id DESC";

$res = $conn->query($sql);
if (!$res) {
    die("Greška u upitu: " . $conn->error);
}
?>

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>Lista Predračuna</title>
  <style>
    body {
      font-family: Arial, sans-serif;
      background-color: #f9f9f9;
      margin: 20px;
    }
    .container {
      max-width: 1000px;
      margin: 0 auto;
      background: #fff;
      border: 1px solid #ccc;
      padding: 20px;
      border-radius: 8px;
    }
    h2 {
      text-align: center;
      margin-top: 0;
    }
    table {
      border-collapse: collapse;
      width: 100%;
      margin: 20px 0;
    }
    th, td {
      border: 1px solid #ccc;
      padding: 8px 12px;
      text-align: center;
    }
    th {
      background: #eee;
    }
    tr:nth-child(even) {
      background-color: #fafafa;
    }
    a {
      text-decoration: none;
      color: #007bff;
      font-weight: 500;
    }
    a:hover {
      text-decoration: underline;
    }
    .btn-new {
      display: inline-block;
      margin: 10px 0;
      padding: 8px 12px;
      background: #28a745;
      color: #fff;
      border-radius: 4px;
      text-decoration: none;
    }
    .btn-new:hover {
      background: #218838;
    }
    .actions a {
      margin-right: 8px;
    }
  </style>
</head>
<body>
<div style="text-align: center; margin: 40px 0;">
    <a href="index.php" style="display: inline-block; padding: 15px 30px; background: #27ae60; color: white; text-decoration: none; border-radius: 8px; box-shadow: 0 3px 6px rgba(0,0,0,0.16);">
        ← Vratite se na početnu
    </a>
</div>
<div class="container">
  <h2>Lista Predračuna</h2>
  <table>
    <thead>
      <tr>
        <th>ID</th>
        <th>Predračun BR.</th>
        <th>Datum</th>
        <th>Klijent</th>
        <th>Iznos (s PDV)</th>
        <th>Status</th>
        <th>Akcije</th>
      </tr>
    </thead>
    <tbody>
      <?php while ($row = $res->fetch_assoc()): ?>
      <tr>
        <td><?php echo $row['id']; ?></td>
        <td><?php echo htmlspecialchars($row['doc_number']); ?></td>
        <td><?php echo htmlspecialchars($row['doc_date']); ?></td>
        <td><?php echo htmlspecialchars($row['naziv_klijenta']); ?></td>
        <td><?php echo number_format($row['total_gross'], 2); ?></td>
        <td><?php echo htmlspecialchars($row['doc_status']); ?></td>
        <td class="actions">
          <a href="izmijeni_predracun.php?id=<?php echo $row['id']; ?>">Izmijeni</a>
          <?php if (strtolower(trim($row['doc_status'])) === 'storno'): ?>
            <span style="color: grey; font-weight: bold;">Storno</span>
          <?php else: ?>
            <a href="lista_predracuna.php?akcija=storno&id=<?php echo $row['id']; ?>"
               onclick="return confirm('Da li ste sigurni da želite storno predračun?');">Storno</a>
          <?php endif; ?>
          <a href="lista_predracuna.php?akcija=obrisi&id=<?php echo $row['id']; ?>"
             onclick="return confirm('Da li ste sigurni da želite obrisati predračun?');">Obriši</a>
          <a href="stampaj_predracun.php?id=<?php echo $row['id']; ?>" target="_blank">Štampaj</a>
        </td>
      </tr>
      <?php endwhile; ?>
    </tbody>
  </table>
  <a href="nova_predracun.php" class="btn-new">+ Novi Predračun</a>
</div>
</body>
</html>
